package com.itboye.textileexpes.repository;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.textileexpes.beans.CopyOfShopCartModel;
import com.itboye.textileexpes.beans.OfferLineOrderBeans;
import com.itboye.textileexpes.beans.ShopCartModel;
import com.itboye.textileexpes.interfaces.IShopCart;
import com.itboye.textileexpes.json.ByJsonRequest.Builder;
import com.itboye.textileexpres.base.BaseErrorListener;
import com.itboye.textileexpres.base.BaseNetRepository;
import com.itboye.textileexpres.base.BaseSuccessReqListener;

public class ShopCartRepository extends BaseNetRepository implements
		IShopCart<ShopCartModel> {
	static final String QUERYALLSHOPCART = "By_ShoppingCart_query";
	static final String ADDSHOPCART = "By_ShoppingCart_add";
	static final String UpdateSHOPCART = "By_ShoppingCart_update";
	static final String DeleteSHOPCART = "By_ShoppingCart_delete";
	// dingdan
	static final String ShopcarOrder = "By_IntentionalOrder_query";

	public ShopCartRepository(ICompleteListener listener) {
		// TODO Auto-generated constructor stub
		this.setListener(listener);

	}

	@Override
	public void add(ShopCartModel model) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<List<ShopCartModel>>() {
		}.getType();
		params.put("uid", model.getUid());
		params.put("id", model.getId());
		params.put("sku_pkid", model.getPsku_id());
		params.put("count", model.getCount());
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ADDSHOPCART, apiVer, params,
						String.class)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void delete(ShopCartModel model) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<ShopCartModel>() {
		}.getType();
		params.put("uid", model.getUid());
		params.put("id", model.getId());
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(DeleteSHOPCART, apiVer, params,
						String.class)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void update(ShopCartModel model) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<ShopCartModel>() {
		}.getType();
		params.put("uid", model.getUid());
		params.put("id", model.getId());
		params.put("count", model.getCount());
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(UpdateSHOPCART, apiVer, params,
						String.class)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getInfo(ShopCartModel model) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<List<ShopCartModel>>() {
		}.getType();
		params.put("uid", model.getUid());
		// 目前最简化调用
		// START
		(new Builder<List<ShopCartModel>>())
				.setTypeVerParamsAndReturnClass(QUERYALLSHOPCART, apiVer,
						params, type)
				.requestListener(
						new BaseSuccessReqListener<List<ShopCartModel>>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public List<ShopCartModel> query(ShopCartModel model) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onShopCharOrder(String uid, String page_size, String page_index) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<OfferLineOrderBeans>() {
		}.getType();
		params.put("uid", uid);
		params.put("page_size", page_size);
		params.put("page_index", page_index);
		// 目前最简化调用
		// START
		(new Builder<OfferLineOrderBeans>())
				.setTypeVerParamsAndReturnClass(ShopcarOrder, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<OfferLineOrderBeans>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

}
